using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Windows.Forms;

using FreeFlow.WinFormsControls.HelperClasses;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Component to check for updates for an application.
	/// </summary>
	public class UpdateChecker : System.ComponentModel.Component
	{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components;

    public UpdateChecker(System.ComponentModel.IContainer container)
    {
      ///
      /// Required for Windows.Forms Class Composition Designer support
      ///
      container.Add(this);
      InitializeComponent();
    }

    public UpdateChecker()
    {
      ///
      /// Required for Windows.Forms Class Composition Designer support
      ///
      InitializeComponent();
    }

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      try
      {
        if( disposing )
        {
          if(components != null)
          {
            components.Dispose();
          }
        }
      }
      finally
      {
        base.Dispose( disposing );
      }
    }


    #region Component Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      components = new System.ComponentModel.Container();
    }
    #endregion

    private string url;
    public string Url
    {
      get
      {
        return url;
      }
      set
      {
        url = value;
      }
    }

    public string GetLatestVersion()
    {
      HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(new Uri(url));

      // Sends the HttpWebRequest and waits for the response.
      HttpWebResponse myHttpWebResponse = myReq.GetResponse() as HttpWebResponse;
      try
      {
        Stream response = myHttpWebResponse.GetResponseStream();
        StreamReader readStream = new StreamReader(response, System.Text.Encoding.GetEncoding("utf-8"));
        return readStream.ReadToEnd();
      }
      finally
      {
        // Releases the resources of the response.
        myHttpWebResponse.Close();
      }
    }

    public void CheckLatestVersion()
    {
      string latestVersion;
      string currentVersion;
      using (new CursorHandler())
      {
        latestVersion = GetLatestVersion();
        currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
      }
      using (NewVersionAvailableForm form = new NewVersionAvailableForm())
      {
        if (latestVersion == currentVersion)
          form.Message = "You are using the latest version";
        else
          form.Message = string.Format(
            "The latest available version is {0}\nYou are using version {1}",
            latestVersion, currentVersion);
        form.ShowDialog();
      }
    }
  }
}
